package com.tangdi.Action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.tangdi.ReportFileInfo;
import com.tangdi.Utils;

public class dwGZ_FF_HZAction  extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(dwGZ_FF_HZAction.class);
	public dwGZ_FF_HZAction() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}
	
	public void init() throws ServletException {
		// Put your code here
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String urlJsp = "dwGZ_FF_HZShow.jsp";
		String pdf_url_root = Utils.getFilePath("root_path");//ReportFileInfo.ROOT_PATH.getName();

		String type = request.getParameter("type");
		String year_month = request.getParameter("year_month");
		
		HttpSession session=request.getSession();
		
		if(year_month != null && type.equals("2")){
			String folder_Name = ReportFileInfo.FOLDER_NAME_GZDWHZ.getName();
			String file_Name = ReportFileInfo.FILE_NAME_GZDWHZ.getName();
			String file_Url = year_month+ "/pdf/" + folder_Name + "/" + year_month + "_" + file_Name;
			String file_Url_excel = year_month+ "/excel/" + folder_Name + "/" + year_month + "_" + file_Name;
			logger.info("单位工资发放汇总表" + year_month + " | " + file_Url);			
			logger.info(pdf_url_root + file_Url + ".pdf");
			
			session.setAttribute("file_Url",file_Url);
			session.setAttribute("file_Url_excel",file_Url_excel);
			session.setAttribute("year_month",year_month);
			
			response.sendRedirect(urlJsp);
			
		}else if(type.equals("3")){//打印
			String msg = "success";
			PrintWriter out=response.getWriter();
			String filePathString = request.getParameter("file_paths");
			System.out.println("filePathString:" + filePathString);
			String[] filePaths = filePathString.split(",");
			for(int i = 0; i < filePaths.length; i++){
				filePaths[i] = Utils.getFilePath("root_path") + filePaths[i];
				//filePaths[i] = ReportFileInfo.ROOT_PATH.getName() + filePaths[i];
				logger.info(filePaths[i]);
			}
			String print_result = "";
			try{
				Runtime.getRuntime().exec("cmd.exe /C start acrord32 /P /o " + filePaths[0]);
				//print_result = UtilsPrintPDF.printPDF(filePaths);
			}catch(Exception e){
				e.printStackTrace();
				msg = "Failure";
				out.print(msg);
				out.flush();
				out.close();
			}
			msg = print_result;
			
			System.out.println("msg:" + msg);
			out.print(msg);
			out.flush();
			out.close();
			
		}else{
			session.setAttribute("file_Url",null);
			response.sendRedirect(urlJsp);
		}
	}
}
